<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 使用el-tree组件展示主题内容 -->
        <!-- :default-expand-all="true" 默认展开所有的子节点 -->
        <el-tree
          :data="list"
          :props="{label:'name',children:'children'}"
          :default-expand-all="true"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">

                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="del(data.id)">删除部门</el-dropdown-item>

                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>
      <!-- 添加弹层 -->
      <el-dialog
        :title="isEdit? '编辑':'添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >

        <AddorEdit
          v-if="showDialog"
          ref="deptDialog"
          :cur-id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="hClose"
        />

      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranDataToTree } from '@/utils/index'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit

  },
  data() {
    return {
      originList: [],
      isEdit: false, // 判断编辑部门的显示与隐藏
      curId: '',
      showDialog: false,
      list: [
        {
          name: '财务部',
          manager: '刘备',
          children: [
            {
              name: '财务核算部',
              manager: '张飞'
            },
            {
              name: '税务核算部',
              manager: '关羽'
            }
          ]
        }]

    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 删除id数据
    del(id) {
      this.$confirm('您确定要删除这条选项嘛').then(
        async() => {
          const res = await delDepartment(id)
          if (res.code !== 10000) return this.$message.error(res.message)
          this.$message.success(res.message)
          // 重新渲染页面
          this.loadDepartments()
          console.log(res, 'delDepartment')
          // 重新
        }).catch(err => err)
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id// 拿到点击的id数据
      this.isEdit = true// 显示编辑状态弹层
      this.showDialog = true
    },
    // 关闭弹层 从新渲染页面
    hClose() {
      this.showDialog = false
      this.loadDepartments()
      this.$refs.deptDialog.resetform()
    },
    // 添加部门
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
      console.log(11111111)
    },
    // 获取组织架构信息列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        if (res.code !== 10000) return this.$message.error(res.message)
        this.originList = res.data.depts.map(({ id, pid, code, name }) => {
          return { id, name, code, pid }
        })
        this.$message.success(res.message)
        console.log('getDepartments', res)
        // shift 从数组头部取出一个元素
        res.data.depts.shift()
        this.list = tranDataToTree(res.data.depts)
        // 保存数据
      } catch (err) {
        console.log('getDepartments', err)
      }
    }

  }
}

</script>
